package com.marianmedla.jane16.lib;

import java.io.BufferedReader;
import java.io.FileReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.TreeMap;
import java.util.Iterator;

public class U {

	 
	public static String toHex(byte[] digest){
		StringBuffer hexString = new StringBuffer();
        String plainText = "";
		for (int i = 0; i < digest.length; i++) {
		    plainText = Integer.toHexString(0xFF & digest[i]);

		    if (plainText.length() < 2) {
		        plainText = "0" + plainText;
		    }

		    hexString.append(plainText);
		}
        return hexString.toString();
	}
	
	public static  String loadTextRaw(String file) throws Exception {

        BufferedReader in = new BufferedReader( new FileReader( file ) );
        String s, s2 = new String();
        while ((s = in.readLine()) != null){
            s2 += s;
        }
        
        return s2;
    
	}
	
	 /**
	 * Redistribute given text to one, two, three, four (and so on) phrases.
	 * Example: 
	 *  1-on the
     *  3-to rely on the
     *  2-on the advantage
     *  1-advantage that  etc.
	 */
	public static void prepareText(int filter, String text, MessageDigest md5, ArrayList<Phrase> preparedText, int items){
		 
		 filter += 1;
	     String[] words = text.split(" ");
	      
	     
	     //Number of words phrases to be uses, every index represent number of words
	     //0=one word, 1=two words,2=three words etc.  (one words phrases are not used though)
	     ArrayList[] buffers = new ArrayList[items];
	     for (int i = 0; i < buffers.length; i++) {
	    	 buffers[i] = new ArrayList<Word>();
		 }
	     
	     TreeMap<Integer,ArrayList<Phrase>> preFinal = new TreeMap<Integer,ArrayList<Phrase>>();
	     
	     for (int i = 0; i <= words.length; i++) {
	    	 
	    	for (int k = 0; k < buffers.length; k++) {
		    	
	    		if(buffers[k].size() == k && (k) > filter){ 
			    	
	    			ArrayList<Word> arr =(ArrayList<Word>)buffers[k];
	    			//StringBuilder sb = new StringBuilder(); 
	    			
	    			Word[] wrds = new Word[arr.size()];
	    			arr.toArray(wrds);
	    			Phrase phrase = new Phrase(wrds);
	    			phrase.position = i;
			    	
	    			if(preFinal.containsKey(k)){
	    				ArrayList<Phrase> a = preFinal.get(k);
	    				a.add(phrase);
	    				preFinal.put(k,a);
	    			}else{
	    				ArrayList<Phrase> a = new ArrayList<Phrase>();
	    				a.add( phrase );
	    				preFinal.put(k,a);
	    			}
	    			
	    			
	    			Iterator<Word> iter = arr.iterator();
	    			boolean remove = false;
	    			while(iter.hasNext()){
	    				Word w = iter.next();
	    				//sb.append(w.text).append(" "); 
	    				if(!remove){
		    				iter.remove();
		    				remove = !remove;
		    		    }
	    			}
	    		
	    		}
	    	}
	    	 
	    	if(  i+1 > words.length ){
	    		break; //it's neccessary to finish here explicitely
	    	}
	    	
            //Temp buffs for another loop, except last one
	    	for (int j = 0; j < buffers.length; j++) {
	    		if(words[i] != null && words[i].trim().length()>0){
	    		   buffers[j].add( new Word(words[i], i, md5) ) ;
	    		}
			}
		    
		 }
	     
	     //preparedText.add( k+"-"+sb.toString().trim() );
	     //for (Integer key : preFinal.keySet()) {
	     for(int i=buffers.length-1; i>0; i--){
	    	  ArrayList<Phrase> p = preFinal.get(i);
	    	  if(p!=null && i > filter)
	    	      preparedText.addAll( p );
		 }
	      
	     
	}
	
 
 
}
